﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.DTO;
using HRProject.Code.Common;

namespace HRProject
{
    public partial class AjaxRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.Params["id"];
            string operation = Request.Params["operation"];
            string status = "true";
            int errorCode = 0;
            int updateResult = 0;
            string error = "";

            JobTitleDAO jobTitleDao = new JobTitleDAO();
            BLJobTitle jobTitle = new BLJobTitle();
            try
            {
                jobTitle.Id = new Guid(id);
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
                return;
            }
            if ("rename_node".Equals(operation))
            {
                jobTitle.Name = Request.Params["title"];
                updateResult = jobTitleDao.RenameJobTitle(jobTitle, ref errorCode);
            }
            if ("rename_node".Equals(operation))
            {
                jobTitle.Name = Request.Params["title"];
                updateResult = jobTitleDao.RenameJobTitle(jobTitle, ref errorCode);
            }
            else if ("remove_node".Equals(operation))
            {
                updateResult = jobTitleDao.DeleteJobTitle(jobTitle, ref errorCode);
            }
            else if ("move_node".Equals(operation))
            {
                jobTitle.Parent = new BLJobTitle();
                try
                {
                    jobTitle.Parent.Id = new Guid(Request.Params["ref"]);
                }
                catch (Exception exp)
                {
                    Console.Out.WriteLine(exp.Message);
                }
                updateResult = jobTitleDao.MoveJobTitle(jobTitle, ref errorCode);
            }
            else if ("create_node".Equals(operation))
            {
                jobTitle.Name = Request.Params["title"];
                jobTitle.Description = string.Empty;
                jobTitle.Parent = new BLJobTitle();
                jobTitle.Parent.Id = jobTitle.Id;
                jobTitle.Id = Guid.NewGuid();
                id = jobTitle.Id.ToString();
                updateResult = jobTitleDao.AddJobTitle(jobTitle, ref errorCode);
            }

            if (updateResult < 1)
            {
                status = "false";
                if (errorCode == 12)
                {
                    error = ContentProvider.GetMessage("NotAuthorized");
                }
            }
            string result = "{\"status\":" + status + ",\"id\":\"" + id + "\",\"error\":\"" + error + "\"}";
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
             
        }
    }
}
